package com.ruoyi.project.hidden.hiddenName.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.web.quesInfo.domain.QuesInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.hidden.hiddenName.mapper.HiddenNameMapper;
import com.ruoyi.project.hidden.hiddenName.domain.HiddenName;
import com.ruoyi.project.hidden.hiddenName.service.IHiddenNameService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 风险名称Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-23
 */
@Service
public class HiddenNameServiceImpl implements IHiddenNameService {
    @Autowired
    private HiddenNameMapper hiddenNameMapper;

    /**
     * 查询风险名称
     *
     * @param riskId 风险名称ID
     * @return 风险名称
     */
    @Override
    public HiddenName selectHiddenNameById(Long riskId) {
        return hiddenNameMapper.selectHiddenNameById(riskId);
    }

    /**
     * 查询风险名称列表
     *
     * @param hiddenName 风险名称
     * @return 风险名称
     */
    @Override
    public List<HiddenName> selectHiddenNameList(HiddenName hiddenName) {
        return hiddenNameMapper.selectHiddenNameList(hiddenName);
    }

    /**
     * 新增风险名称
     *
     * @param hiddenName 风险名称
     * @return 结果
     */
    @Override
    public int insertHiddenName(HiddenName hiddenName) {
        return hiddenNameMapper.insertHiddenName(hiddenName);
    }

    /**
     * 修改风险名称
     *
     * @param hiddenName 风险名称
     * @return 结果
     */
    @Override
    public int updateHiddenName(HiddenName hiddenName) {
        return hiddenNameMapper.updateHiddenName(hiddenName);
    }

    /**
     * 删除风险名称对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHiddenNameByIds(String ids) {
        return hiddenNameMapper.deleteHiddenNameByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除风险名称信息
     *
     * @param riskId 风险名称ID
     * @return 结果
     */
    @Override
    public int deleteHiddenNameById(Long riskId) {
        return hiddenNameMapper.deleteHiddenNameById(riskId);
    }

    @Override
    public String importEntity(List<HiddenName> nameList, Long sysId, Long linkId) {
        if (StringUtils.isNull(nameList) || nameList.size() == 0) {
            throw new BusinessException("导入题目数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (HiddenName name : nameList) {
            name.setLinkId(linkId);
            name.setSysId(sysId);
            try {
                HiddenName item = hiddenNameMapper.selectIsExit(name);
                // 验证是否存在这个用户
                if (StringUtils.isNull(item)) {
                    this.insertHiddenName(name);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、隐患名称:" + name.getRiskName() + " 导入成功");
                } else {
                    name.setRiskId(item.getRiskId());
                    this.updateHiddenName(name);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、隐患名称:" + name.getRiskName() + " 更新成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、隐患名称: " + name.getRiskName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
